from PIL import Image
dice_numbers = []
COL = 5                 
ROW = 2                 
UNIT_HEIGHT_SIZE = 60               
UNIT_WIDTH_SIZE = 60                
PATH = ""               
SAVE_QUALITY = 75           

def concat_images(name, path, dice):
    """_summary_
        concat images 
    Args:
        name (_str_): _name of final pics_
        path (_type_): _local file path_
        dice (_type_): _the dice numbers_
    """
    image_files = []
    for index in range(6):
        image_files.append(Image.open(path + str(index + 1) + ".png")) 
    target = Image.new('RGB', (UNIT_WIDTH_SIZE * COL, \
    UNIT_HEIGHT_SIZE * ROW)) 
    
    for row in range(ROW):
        for col in range(COL):
            target.paste(image_files[int(dice[row][col]) - 1], \
                (0 + UNIT_WIDTH_SIZE*col, 0 + UNIT_HEIGHT_SIZE*row))
    target.save(path + name + '.jpg', quality = SAVE_QUALITY) 
     # save final pics

if __name__ == '__main__':
 
    img = Image.open("Toys/GenerativeArt/artPkgs/demo.png") 
    img_array = img.load() # load pics

    x = 4  
    y = 4  
    for j in range(int(img.height / y)):
        accesstory = []
        for k in range(int(img.width / x)):
            a = 0
            for l in range(x):
                for m in range(y):
                    a = (img_array[x * k + l, y * j + m][0] +
                         img_array[x * k + l, y * j + m][1] +
                         img_array[x * k + l, y * j + m][2]) / 3 + a
            a = a / (x * y)  
            if 0 < a <= 42.5:
                accesstory.append("6")
            elif 42.5 < a <= 85:
                accesstory.append("5")
            elif 85 < a <= 127.5:
                accesstory.append("4")
            elif 127.5 < a <= 170:
                accesstory.append("3")
            elif 170 < a <= 212.5:
                accesstory.append("2")
            elif 212.5 < a <= 255:
                accesstory.append("1")
        dice_numbers.append(accesstory)

        print("")
    ROW = len(dice_numbers)
    COL = len(dice_numbers[0])
    concat_images("resultpic", "Toys/GenerativeArt/Pixs/", dice_numbers)

    # print("---" * 40)
    # print("\n 骰子点数")
    # with open("Toys/GenerativeArt/results.txt", "w+" ) as f:
    #     for i in dice_numbers:
    #         print("".join(i))
    #         f.writelines("".join(i) + "\n")